package cn.fan.service;

import cn.fan.config.RxiongurlProperties;
import cn.fan.pojo.rxiong.RXFundData;
import cn.fan.pojo.rxiong.RXFundMain;
import cn.fan.pojo.rxiong.RXR;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class RxiongService {

    @Autowired
    private RxiongurlProperties rxiongurl;

    @Autowired
    private RestTemplate restTemplate;

    // 获取所有基金信息
    public List<RXFundMain> getAll(String keyWord){
        String url = rxiongurl.getFundall();
        if(StringUtils.hasText(keyWord))
            url += "?keyWord="+keyWord;

        RXR rxr = restTemplate.getForObject(url, RXR.class);
        if(rxr.getCode() != 200)
            return new ArrayList<>();

        String dataJson = JSON.toJSONString(rxr.getData());

        JSONArray list = JSONObject.parseObject(dataJson, JSONArray.class);
        List<RXFundMain> rxFundMains=parseRXFList(list,RXFundMain.class);
        return rxFundMains;
    }

    // 获取指定基金（集合） 指定区间内的基金明细
    public List<RXFundData> getlist(Map<String,Object> map){
        String url = rxiongurl.getFundlist();

        if(null == map.get("codes") || "".equals(map.get("codes")))
            return new ArrayList<>();;

        url += "?code="+map.get("codes").toString();
        if(null != map.get("startDate"))
            url += "&startDate="+map.get("startDate").toString();
        if(null != map.get("endDate"))
            url += "&endDate="+map.get("endDate").toString();

        RXR rxr = restTemplate.getForObject(url, RXR.class);
        if(rxr.getCode() != 200)
            return new ArrayList<>();;

        String dataJson = JSON.toJSONString(rxr.getData());
//        System.out.println(dataJson);


        JSONArray list = JSONObject.parseObject(dataJson, JSONArray.class);
        System.out.println("size:"+list.size());
        List<RXFundData> rxFundDataList = parseRXFList(list,RXFundData.class);
        return rxFundDataList;
    }


//    private List<RXFundMain> parseRXFList(JSONArray list){
//        if (list.size()==0)
//            return new ArrayList<>();
//
//        List<RXFundMain> rxFundMains = new ArrayList<>();
//        for(Object rxfJson : list){
//            RXFundMain rxFundMain = JSONObject.parseObject(rxfJson.toString(), RXFundMain.class, Feature.SupportArrayToBean);
//            rxFundMains.add(rxFundMain);
//        }
//        return rxFundMains;
//    }

    private <T> List<T> parseRXFList(JSONArray list,java.lang.Class<T> clazz){
        if (list.size()==0)
            return new ArrayList<>();

        List<T> result = new ArrayList<>();
        for(Object rxfJson : list){
            T rxFundMain = JSONObject.parseObject(rxfJson.toString(),clazz, Feature.SupportArrayToBean);
            result.add(rxFundMain);
        }
        return result;
    }


}
